<template>
  <div class="detail">
    <img :src="detailData.imgUrl" alt="" />
    <div class="title">
      <p
        style="
          font-weight: 800;
          font-size: 15px;
          color: black;
          margin-bottom: 5px;
        "
      >
        {{ detailData.name }}
      </p>
      <p>月售{{ detailData.sellCount }} 好评率{{ detailData.rating }}%</p>
      <div class="price">
        <span>￥{{ detailData.price }}</span>
        <button id="btn" @click="addToCart">加入购物车</button>
      </div>
    </div>
    <div class="text">
      <p class="title" style="font-weight: 800; font-size: 15px; color: black">
        商品介绍
      </p>
      <p>{{ detailData.goodsDesc }}</p>
    </div>
    <ul class="tab">
      <li @click="setTab('all')">全部</li>
      <li @click="setTab('recommend')">推荐</li>
      <li @click="setTab('complain')">吐槽</li>
    </ul>
    <div class="content">
      <div class="item" v-for="item in filteredRatings" :key="item.id">
        <img :src="item.avatar" alt="" />
        <div class="item-text">
          <div class="text-top">
            <span>{{ item.username }}</span>
            <span>{{ item.rateTime }}</span>
          </div>
          <div class="text-bot">{{ item.text }}</div>
        </div>
      </div>
    </div>
    <div class="footer">
      <footer-component></footer-component>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from "vue";
import { storeToRefs } from "pinia";
import { useCartStore } from "@/stores/cart.js"; // 引入 Pinia 仓库
const cartStore = useCartStore(); // 使用 Pinia 仓库
const { detailData } = storeToRefs(cartStore); // 将 cartList 转换为响应式数据
const currentTab = ref("all");
const addToCart = () => {
  cartStore.addCartItem(detailData.value);
};

const filteredRatings = computed(() => {
  if (currentTab.value === "all") {
    return detailData.value.ratings;
  } else if (currentTab.value === "recommend") {
    return detailData.value.ratings.filter((r) => r.rateType === 0);
  } else if (currentTab.value === "complain") {
    return detailData.value.ratings.filter((r) => r.rateType === 1);
  }
  return [];
});

const setTab = (tab) => {
  currentTab.value = tab;
};
</script>

<style lang="less">
.detail {
  width: 100vw;
  height: 100vh;
  font-size: 12px;
  p {
    margin-left: 10px;
    font-size: 11px;
    color: #939393;
  }
  img {
    width: 100%;
    height: 50vh;
    margin-bottom: 10px;
  }
  .price {
    padding: 10px;
    align-items: center;
    width: 100%;
    justify-content: space-between;
    display: flex;
    border-bottom: 2px solid #f5f5f5;
    #btn {
      margin-right: 20px;
      border: none;
      font-size: 11px;
      background-color: #3c79fd;
      color: #fff;
      border-radius: 15px;
      padding: 8px 10px;
    }
    span {
      color: red;
      font-weight: 700;
    }
  }

  .text {
    border-bottom: 2px solid #f5f5f5;
    padding-bottom: 18px;
    .title {
      margin: 10px 10px;
    }
  }
  .tab {
    height: 35px;
    display: flex;
    border-bottom: 2px solid #f5f5f5;
    li {
      margin: auto 5px;
      width: 52px;
      height: 80%;
      background: #3c79fd;
      font-size: 11px;
      color: #c8eef7;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  .content {
    margin-bottom: 70px;
    .item {
      border: 1px solid #f0f0f0;
      display: flex;
      img {
        width: 45px;
        height: 45px;
        margin: 10px;
        border-radius: 50%;
      }
      .item-text {
        margin: 10px 0;
        width: 100%;
        .text-top {
          display: flex;
          justify-content: space-between;
          align-items: center;
        }
        .text-bot {
          color: #353535;
        }
      }
    }
  }
  .footer {
    width: 100vw;
    height: 40px;
    background: rgb(9, 9, 9);
    position: fixed;
    bottom: 0;
    z-index: 9;
  }
}
</style>
